package com.cham.redis.controller;

import com.cham.redis.utils.RedisUtils;
import com.cham.video.dto.VideoInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @author Howl-Xu
 * @version 1.0.0
 * @Description: 请描述一下这个类文件吧
 * @createTime 2020年03月15日 12:23:00
 */
@Controller
@RequestMapping("/redisController")
public class RedisController {

    /**
     * 使用Jedis连接池，提高性能
     */
    @Autowired
    private JedisPool jedisPool;

    @PostMapping("/saveVideoInfoListCache")
    @ResponseBody
    public void saveVideoInfoListCache(@RequestParam String objectName, @RequestBody List<VideoInfo> videoInfoList){
        Jedis jedis = jedisPool.getResource();
        RedisUtils.saveListObject(jedis, objectName, videoInfoList);
        jedis.close();
        System.out.println("Redis写入缓存");
    }

    @GetMapping("/getVideoInfoListCache")
    @ResponseBody
    public List<VideoInfo> getVideoInfoListCache(String objectName){
        Jedis jedis = jedisPool.getResource();
        List<VideoInfo> list = RedisUtils.getListObject(jedis, objectName, VideoInfo.class);
        jedis.close();
        System.out.println("Redis读取缓存");
        return list;
    }
}
